package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.entity.Company;
import com.blb.entity.Par;
import com.blb.entity.Relation;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ICompanyService;
import com.blb.service.IParService;
import com.blb.service.IRelationService;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-15
 */
@Controller
@RequestMapping("/par")
public class ParController {

    //通过session获取当前的公司ID
    private static User user;

    @Resource
    private IParService parService;

    @Resource
    private ICompanyService companyService;

    @Resource
    private IRelationService relationService;


    @RequiresPermissions(value = {"apply", "audit"}, logical = Logical.OR)
    @RequestMapping("{page}")
    public String aaa(@PathVariable String page){
        user=ShiroUtils.getUser();
        return "customer/"+page;
    }

    /**
     * 模糊查询我审核的公司
     * @param name 公司名称
     * @param startTime 开始申请时间
     * @param endTime 结束申请时间
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("audit")
    @ResponseBody
    @RequestMapping("findInfo")
    public BlbPage selectInfo(@RequestParam(required = false, value = "comName") String name, @RequestParam(required = false, value = "startTime")String startTime, @RequestParam(required = false, value = "endTime") String endTime, int start, int length){
        user=ShiroUtils.getUser();
        List<Par> list = parService.selInfo(user.getCompanyId(),name,startTime,endTime,start,length);
        int count = parService.getSelfCount(user.getCompanyId(), name, startTime, endTime);
        return new DataTablePage(count,count,list);
    }

    /**
     *查看我申请的公司的详细信息
     * @param companyId 我申请的公司的Id
     * @param companyRel 我申请的公司与我的公司之间的关系
     * @param model
     * @return
     */
    @RequiresPermissions("apply")
    @RequestMapping("selfRelation")
    public String getSelfRelation(String companyId,String companyRel,Model model){
        //首先获取到自己公司的ID通过shiro可以获取到
        //然后获取到对方公司的ID,通过relation判断我是申请为什么状态
        Par par = parService.selSelfApply(user.getCompanyId(), companyId,companyRel);
        //将公司实体放入par中
        par.setCompany(companyService.getOne(new QueryWrapper<Company>().eq("id", par.getTParComB())));
        model.addAttribute("par",par);
        return "customer/clientele_coopsee.html";
    }

    /**
     * 模糊查询我申请的公司
     * @param startTime 开始申请时间
     * @param endTime 结束申请时间
     * @param start
     * @param length
     * @return
     */

    @ResponseBody
    @RequiresPermissions("apply")
    @RequestMapping("findInfoA")
    public BlbPage selectInfoByApply(@RequestParam(required = false, value = "startTime")String startTime,@RequestParam(required = false, value = "endTime") String endTime, int start, int length){
        user=ShiroUtils.getUser();
        List<Par> list = parService.selInfoByApply(user.getCompanyId(),startTime,endTime,start,length);
        for (Par par:list) {
            par.setCompany(companyService.getOne(new QueryWrapper<Company>().eq("id", par.getTParComB())));
        }
        int count = parService.getOtherCount(user.getCompanyId(), startTime, endTime);
        return new DataTablePage(count,count,list);
    }

    /**
     * 查看我审核的公司的详细信息
     * @param companyId 我审核的公司的Id
     * @param companyRel 我审核的公司与我的公司之间的关系
     * @param model
     * @return
     */
    @RequiresPermissions("audit")
    @RequestMapping("otherRelation")
    public String getOtherRelation(String companyId,String companyRel,Model model){
        //首先获取到自己公司的ID通过shiro可以获取到
        //然后获取到对方公司的ID,通过relation判断我是申请为什么状态
        Par par = parService.selSelfApply(companyId, user.getCompanyId(),companyRel);
        par.setCompany(companyService.getOne(new QueryWrapper<Company>().eq("id", par.getTParComA())));
        model.addAttribute("par",par);
        return "customer/clientele_audit.html";
    }

    /**
     * 审核时通过申请，将他设置为我的客户
     * @param companyId
     * @param intro
     * @param relations
     * @return
     */
    @RequiresPermissions("audit")
    @RequestMapping("acceptCus")
    public String customerAccept(String companyId,String intro,String relations){
        Relation relation = new Relation();
        relation.setId(UUID.randomUUID().toString().replace("-",""));
        relation.setRSyno(intro);
        relation.setCustomerId(companyId);
        relation.setSupplierId(user.getCompanyId());
        relationService.save(relation);

        Par p = new Par();
        p.setStaffName(user.getUserName());
        p.setTParStatus2("通过");
        p.setTParStatus("已同意");
        parService.update(p,new UpdateWrapper<Par>().eq("t_par_com_a",companyId).eq("t_par_com_b",user.getCompanyId()).eq("t_par_rel",relations));
        return "customer/clientele_auditing.html";
    }

    /**
     * 审核时通过申请，将他设置为我的供应商
     * @param companyId
     * @param intro
     * @param relations
     * @return
     */
    @RequiresPermissions("audit")
    @RequestMapping("acceptSup")
    public String supplierAccept(String companyId,String intro,String relations){
        Relation relation = new Relation();
        relation.setId(UUID.randomUUID().toString().replace("-",""));
        relation.setRSyno(intro);
        relation.setCustomerId(user.getCompanyId());
        relation.setSupplierId(companyId);
        relationService.save(relation);
        Par p = new Par();
        p.setStaffName(user.getUserName());
        p.setTParStatus2("通过");
        p.setTParStatus("已同意");
        parService.update(p,new UpdateWrapper<Par>().eq("t_par_com_a",companyId).eq("t_par_com_b",user.getCompanyId()).eq("t_par_rel",relations));
        return "customer/clientele_auditing.html";
    }

    /**
     * 审核时拒绝申请
     * @param Id
     * @param desc
     * @param relations
     * @return
     */
    @RequiresPermissions("audit")
    @RequestMapping("refuseApply")
    public String refuseApply(String Id,String desc,String relations){
        Par par = new Par();
        par.setId(Id);
        par.setTAprDesc(desc);
        par.setStaffName(user.getUserName());
        par.setTParStatus("已拒绝");
        par.setTParStatus2("拒绝");
        par.setTParRel(relations);
        parService.updateById(par);
        return "customer/clientele_auditing.html";
    }
}

